import sys

sys.path.append(r"/data/finance.Source/utils")
#sys.path.append(r"/data/finance.Source/Analysis/static")
#sys.path.append(r"/data/finance.Source/Analysis/shortempty")

import sqlutils
#import codes
import datetimeutils
#from Shortemptybase import Bigbs

# 查询莫一天后分笔数据库
def selectNum(codeitem,lastdate):
    results = {}
 #   bigbsitem = Bigbs(codeitem,dnum)
    connection = sqlutils.connect(dbname='futures')
    try:
        with connection.cursor() as cursor:
#            lastdate = datetimeutils.getlastday(dnum)
            sql = "select * from " + codeitem['table'] + " where ContractCode='" + codeitem['code'] +"' and Volume >" + codeitem['bigNum'] + " and (OpenVol  > " + codeitem['bigNum'] + " or CloseVol> " + codeitem['bigNum']+") and Time > '" + lastdate.strftime("%Y-%m-%d %H:%M:%S") +"'"
            print(sql)
            cursor.execute(sql)
            results = cursor.fetchall()
 #           bigbsitem.ergodicData(results)
 #           bigbsitem.summary()

    finally:
        connection.close()

 #   bigbsitem.printData()
    return results


# 查询莫一天的分笔数据库
def selectoneNum(codeitem,sqldunm):
    results = {}
 #   bigbsitem = Bigbs(codeitem,dnum)
    connection = sqlutils.connect(dbname='futures')
    try:
        with connection.cursor() as cursor:
            lastdate = datetimeutils.getlastday(sqldunm)
            sqlenddum = sqldunm +1
            endate = datetimeutils.getlastday(sqlenddum)
            sql = "select * from " + codeitem['table'] + " where ContractCode='" + codeitem['code'] +"' and Volume >" + codeitem['bigNum'] + " and (OpenVol  > " + codeitem['bigNum'] + " or CloseVol> " + codeitem['bigNum']+") and Time > '" + lastdate.strftime("%Y-%m-%d %H:%M:%S") +"' and Time < '" + endate.strftime("%Y-%m-%d %H:%M:%S") + "'"
            print(sql)
            cursor.execute(sql)
            results = cursor.fetchall()
 #           bigbsitem.ergodicData(results)
 #           bigbsitem.summary()

    finally:
        connection.close()

 #   bigbsitem.printData()
    return results
#selectNum(codes.TA905,-5)

# 查最后一天收盘价
def selectDKline(code):
    connection = sqlutils.connect(dbname='futures')
    dkdata = {}
    try:
        with connection.cursor() as cursor:
            sql = "select * from dailykline where code='" + code +"' order by date desc limit 1"
            cursor.execute(sql)
            dkdata = cursor.fetchall()
    finally:
        connection.close()

    return dkdata

# 查最后莫一天K线数据
def selectonedayDKline(code,onedate):
    connection = sqlutils.connect(dbname='futures')
    dkdata = {}
    try:
        with connection.cursor() as cursor:
            sql = "select * from dailykline where code='" + code +"' and date ='" + onedate.strftime("%Y-%m-%d") + "'"
            print(sql)
            cursor.execute(sql)
            dkdata = cursor.fetchall()
    finally:
        connection.close()

    return dkdata

